<?php
include_once ('../../php/postgre.php');
$act = '';
if (isset($_POST['act']))
    $act = $_POST['act'];
if ($act == 'simpan_prioritas') {
    $id_prioritas = $_POST['id_prioritas'];
    $kode_prioritas = $_POST['kode_prioritas'];
    $nama_prioritas = $_POST['nama_prioritas'];
    if (!is_numeric($id_prioritas)) {
        echo "Err : invalid id.";
        exit;
    }

    if ($id_prioritas > 0) {
        exec_query("update mus_prioritas set id_prioritas=" . $id_prioritas . ", kode_prioritas='" . $kode_prioritas . "', nama_prioritas='" . $nama_prioritas . "' where id_prioritas=" . $id_prioritas);
        echo 'success__';
		logs('Mengubah master Prioritas','Kode Prioritas : '.$kode_prioritas.'; Nama Prioritas : '.$nama_prioritas);
    } else {
        $maxid = get_maxid('id_prioritas', 'mus_prioritas');
        exec_query("insert into mus_prioritas (id_prioritas, kode_prioritas, nama_prioritas) values(" . $maxid . ",'" . $kode_prioritas . "','" . $nama_prioritas . "')");
        $st = get_data('select id_prioritas from mus_prioritas where id_prioritas=' . $maxid);
        if ($st['id_prioritas'] == $maxid) {
            echo 'success__';
            $id_prioritas = $maxid;
			logs('Menambah master Prioritas','Kode prioritas : '.$kode_prioritas.'; Nama Prioritas : '.$nama_prioritas);
        }
    }
    echo $id_prioritas . '__';
    view_prioritas();
} else if ($act == 'hapus_prioritas') {
    $id_prioritas = $_POST['id_prioritas'];
    if (is_numeric($id_prioritas)) {
		$prii = get_data('select kode_prioritas,nama_prioritas from mus_prioritas where id_prioritas='.$id_prioritas);
        exec_query("delete from mus_prioritas where id_prioritas=" . $id_prioritas);
        echo 'success__';
		logs('Menghapus master Prioritas','Kode prioritas : '.$prii['kode_prioritas'].'; Nama prioritas : '.$prii['nama_prioritas']);
        view_prioritas();
    } else {
        echo 'error';
    }
} else if ($act == 'ubah_prioritas') {
    $id_prioritas = $_POST['id_prioritas'];
    if (is_numeric($id_prioritas)) {
        $data = get_data("select id_prioritas, kode_prioritas, nama_prioritas from mus_prioritas where id_prioritas=" . $id_prioritas);
        $ress = implode($data, '__');
        print_r($ress);
        echo "__";
    } else {
        echo 'error';
    }
} else if($act == 'get_kode'){
	$kode = get_data("select count (*) as mac from mus_prioritas");
	$koded = $kode['mac']+1;
	$kodede = ($koded <=9)? "0".$koded:$koded;
	$kodepri = "Prioritas.".$kodede;
	echo $kodepri; 
}

function view_prioritas() {
    $x = 1;
    $pr = get_datas("select * from mus_prioritas");
    foreach ($pr as $pr) {
        ?><tr>
            <td><?php echo $x ?></td>
            <td><?php echo $pr['kode_prioritas'] ?></td>
            <td><?php echo $pr['nama_prioritas'] ?></td>
            <td class="center" >
                        <a href="#modalwin" data-toggle="modal"  class="btn btn-info bt-edit btn-small" name="<?php echo $pr['id_prioritas']; ?>"><i class="icon-edit icon-white"></i> ubah</a>
                        <a class="btn btn-danger bt-hapus btn-small" name="<?php echo $pr['id_prioritas']; ?>"><i class="icon-trash icon-white"></i> hapus</a>

                    </td>
        </tr>
        <?php
        $x++;
    }
}
?>

